package com.bob.web.dao;

import com.bob.web.common.constant.Page;
import com.bob.web.entity.system.Resource;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Repository;

import java.util.List;
import java.util.Set;

/**
 * 名称: ResourceDao<br>
 * 描述:资源持久层<br>
 * 类型: JAVA<br>
 * 最近修改时间:2017/7/20 14:47<br>
 *
 * @author BoYangsh
 * @version [版本号, V1.0]
 * @since 2017/7/20 14:47
 */
//@Repository
public class ResourceDao extends BaseMongoDaoSupport {

	/**
	 * 查找分页数据
	 * @param page
	 */
	public void findPage(Page<Resource> page) {
		Query query = new Query();
		findPage(query, page, Resource.class);
	}

	/**
	 * 添加资源
	 * @param resource
     */
	public void add(Resource resource) {
		insert(resource);
	}

	/**
	 * 查找资源
	 * @param menuLevel
	 * @param fields
	 * @return
	 */
	public List<Resource> findResourcesByMenuLevel(Integer menuLevel, String...fields) {
		Query query = Query.query(Criteria.where("menuLevel").is(menuLevel));
		includes(query, fields);
		query.with(new Sort(Sort.Direction.DESC, "_id"));
		return find(query, Resource.class);
	}

	/**
	 * 删除资源
	 * @param id
     */
	public void remove(String id) {
		Query query = Query.query(Criteria.where("_id").is(id));
		remove(query, Resource.class);
	}

	public List<Resource> findResourcesByAuthorityType(Integer authority, String...fields) {
		Query query = Query.query(Criteria.where("authorityType").is(authority));
		includes(query, fields);
		return find(query, Resource.class);
	}

	public List<Resource> findResourcesByAuthorityTypes(Set<Integer> authorityTypes, String...fields) {
		Query query = Query.query(Criteria.where("authorityType").in(authorityTypes));
		includes(query, fields);
		return find(query, Resource.class);
	}

	public List<Resource> findAllResources(String...fields) {
		return findAll(Resource.class);
	}
}
